// var NTKFSTAT = require('../../src/js/ntkfstat.js');
var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    phone: '', //手机号
    code: '', //验证码
    iscode: null, //用于存放验证码接口里获取到的code
    codename: '获取验证码',
    disabled: true
  },
  getVerificationCode: function() {
    this.getCode();
  },
  getCodeValue: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  getPhoneValue: function(e) {
    var _this = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    _this.setData({
      phone: e.detail.value,
    });
    console.log('666666',e.detail.value)
    if (!this.data.phone == '' && myreg.test(this.data.phone)) {
      _this.setData({
        disabled: false
      })
    } else {
      _this.setData({
        disabled: true
      })
    }
  },
  // 获取验证码
  getCode(options) {
    console.log(options)
    var that = this;
    var mobile = this.data.phone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var that = this;
    if (this.data.phone == '') {
      wx.showToast({
        title: '手机号不能为空！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      util.postUrl('member/bind_sms_captcha', {
        phone: that.data.phone
      }, function(res) {
        if (res) {
          var num = 30;
          var timer = setInterval(function() {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: false
              })
            } else {
              that.setData({
                codename: num + 's后重新发送',
                disabled: true
              })
            }
          }, 1000)
        }

      })

    }
  },
  // 立即注册 提交
  register: function() {
    var _this = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.phone == '') {
      wx.showToast({
        title: '手机号不能为空！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.code == '') {
      wx.showToast({
        title: '请输入获取验证码！',
        icon: 'none'
      })
      return false
    }

    util.postUrl('member/bind_phone', {
      phone: _this.data.phone,
      captcha: _this.data.code,
    }, function(res) {
      if (res) {
        wx.showToast({
          title: '绑定成功',
        })
        wx.navigateBack({

        })
      }
    })
    console.log('验证码', this.data.code)
  },
  // 登录
  login: function() {
    console.log('直接登录')
  },
  onGotUserInfo: function(res) {
    console.log(res.detail)
  },
  // 客服
  kf: function() {

  }
})